package com.tunghuynh.hpbd;

import com.tunghuynh.game.st_avoidball.AvoidBall;
import com.tunghuynh.game.th_flappybird.FlappyBird;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btn1, btnGame1, btnGame2;
	private Animation fadein, fadeout;
	private boolean isOk = false;
	private TextView txtNote;
	private LinearLayout liNote2;
	private MediaPlayer mp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Global.game1IsPassed = false;
		Global.game2IsPassed = false;
		isOk = false;
		final LinearLayout liNote = (LinearLayout) findViewById(R.id.msgMe);
		liNote2 = (LinearLayout) findViewById(R.id.msgMe2);
		btn1 = (Button) findViewById(R.id.btn1);
		btnGame1 = (Button) findViewById(R.id.game1);
		btnGame2 = (Button) findViewById(R.id.game2);
		btn1.setVisibility(View.INVISIBLE);
		btnGame1.setVisibility(View.INVISIBLE);
		btnGame2.setVisibility(View.INVISIBLE);
		
		mp = MediaPlayer.create(MainActivity.this, R.raw.hpbd);

		fadein = new AlphaAnimation(0.00f, 1.00f);
		fadein.setDuration(700);
		fadein.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				btn1.setVisibility(View.VISIBLE);
				if (isOk) {
					btnGame1.setVisibility(View.VISIBLE);
					btnGame2.setVisibility(View.VISIBLE);
				}
				isOk = true;
			}
		});
		fadeout = new AlphaAnimation(1.00f, 0.00f);
		fadeout.setDuration(700);
		fadeout.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				btn1.setVisibility(View.INVISIBLE);
			}
		});

		/**
		 * Load note
		 */
		LayoutInflater li2 = (LayoutInflater) MainActivity.this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li2.inflate(R.layout.layout_msg_blue, liNote, true);
		txtNote = (TextView) liNote.findViewById(R.id.txtMsgBlue);
		txtNote.setText("Xin chào "
				+ Global.username
				+ ", nhân dịp sinh nhật lần thứ 22 của bạn, tôi có món quà muốn tặng bạn. Chúc bạn sinh nhật vui vẻ!\r\n" 
				+ "Để nhận được quà, bạn cần hoàn thành 2 trò chơi đơn giản của tôi.\r\n"
				+ "Bấm đồng ý để hiện trò chơi.\r\n" 
				+ "(Chỉ có duy nhất 1 lựa chọn thôi =)))");
		liNote.setVisibility(View.INVISIBLE);

		liNote.setVisibility(View.VISIBLE);
		liNote.startAnimation(fadein);

		/**
		 * Load note
		 */
		LayoutInflater li22 = (LayoutInflater) MainActivity.this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li22.inflate(R.layout.layout_msg_grey, liNote2, true);
		final TextView txtNote2 = (TextView) liNote2
				.findViewById(R.id.txtMsgGrey);
		txtNote2.setText(":D");
		liNote2.setVisibility(View.INVISIBLE);

		btn1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Global.game1IsPassed && Global.game2IsPassed) {
					Intent int3 = new Intent(MainActivity.this, PlayVideo.class);
					startActivity(int3);
				} else {
					btn1.startAnimation(fadeout);
					liNote2.setVisibility(View.VISIBLE);
					liNote2.startAnimation(fadein);
					btnGame1.setBackgroundResource(R.drawable.bgbtn);
					btnGame2.setBackgroundResource(R.drawable.bgbtn);
					btnGame1.setEnabled(true);
					btnGame2.setEnabled(true);
				}

			}
		});
		btnGame1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent int1 = new Intent(MainActivity.this, AvoidBall.class);
				startActivity(int1);
			}
		});
		btnGame2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent int2 = new Intent(MainActivity.this, FlappyBird.class);
				startActivity(int2);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mp.start();
		btnGame1.setEnabled(!Global.game1IsPassed);
		if (Global.game1IsPassed) {
			btnGame1.setBackgroundResource(R.drawable.bgbtnin);
		} else {
			btnGame1.setBackgroundResource(R.drawable.bgbtn);
		}
		btnGame2.setEnabled(!Global.game2IsPassed);
		if (Global.game2IsPassed) {
			btnGame2.setBackgroundResource(R.drawable.bgbtnin);
		} else {
			btnGame2.setBackgroundResource(R.drawable.bgbtn);
		}

		if (Global.game1IsPassed && Global.game2IsPassed) {
			txtNote.setText("Chúc mừng bạn đã hoàn thành trò chơi!\r\n"
					+ "Giờ thì có thể mở quà được rồi <3");
			liNote2.setVisibility(View.INVISIBLE);
			btn1.setText("Mở quà");
			btn1.setVisibility(View.VISIBLE);
		}

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mp.stop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
